package com.hh.action;

import com.hh.service.impl.PageService;
import com.hh.pojo.Demo;
import com.hh.pojo.PagePojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

@Controller
@RequestMapping("/")
public class TestController {
@Autowired
private PageService pageService;
    @GetMapping("j/index")
    public String jIndex(Model model) {
        System.out.println("jsp index");
        model.addAttribute("msg", "hello jsp:" + LocalDateTime.now());
        return "jsp/index";
    }

    @GetMapping("t/index")
    public String tIndex(Model model) {
        System.out.println("thymeleaf index");
        model.addAttribute("msg", "hello thymeleaf:" + LocalDateTime.now());
        return "thymeleaf/index";
    }

    @GetMapping("f/index")
    public String fIndex(Model model) {
        System.out.println("jsp index");
        model.addAttribute("msg", "hello jsp:" + LocalDateTime.now());
        return "freemarker/index";
    }

    @RequestMapping("j/list")
    public String jList(PagePojo<Demo> page,Model model) {
        PagePojo<Demo> demoPagePojo = pageService.queryPage(page);
        model.addAttribute("page",demoPagePojo);
        return "jsp/index";
    }

    @GetMapping("t/list")
    public String tList(String name,Model model) {
        List<Demo> demos = IntStream.range(1, 20).mapToObj(i -> new Demo(i + "", name + "_name_" + i)).toList();
        model.addAttribute("list",demos);
        return "thymeleaf/index";
    }
    @GetMapping("f/list")
    public String fList(String name,Model model) {
        List<Demo> demos = IntStream.range(1, 20).mapToObj(i -> new Demo(i + "", name + "_name_" + i)).toList();
        model.addAttribute("list",demos);
        return "freemarker/index";
    }

    @PostMapping("save/demo")
    @ResponseBody
    public Map<String,Object> fList(@RequestBody Demo demo) {
        System.out.println(demo);
        return Map.of("success",true);
    }

    @PostMapping("demo/deleteByIds")
    @ResponseBody
    public Map<String,Object> deleteByIds(@RequestBody List<String> ids) {
        System.out.println(ids);
        return Map.of("success",true);
    }

    @PostMapping("demo/saveBath")
    @ResponseBody
    public Map<String,Object> saveBath(@RequestBody List<Demo> demo) {
        System.out.println(demo);
        return Map.of("success",true);
    }




    @RequestMapping("j/dialogIndex")
    public String dialogIndex() {

        return "jsp/dialogIndex";
    }
    @RequestMapping("j/dialogList")
    public String dialogList(@RequestBody Map<String,Object> params,Model model) {
        PagePojo<Demo> page=new PagePojo<Demo>();
        page.setPageNo(1);
        PagePojo<Demo> demoPagePojo = pageService.queryByParamsPage(page,params);
        model.addAttribute("page",demoPagePojo);
        return "jsp/index";
    }
    @RequestMapping("j/showDialogIndex")
    public String showDialogIndex(@RequestBody Map<String,Object> params,Model model) {
        PagePojo<Demo> page=new PagePojo<Demo>();
        page.setPageNo(1);
        PagePojo<Demo> demoPagePojo = pageService.queryByParamsPage(page,params);
        model.addAttribute("page",demoPagePojo);
        return "jsp/showDialogIndex";
    }
  @RequestMapping("j/testgit")
    public String testgit(@RequestBody Map<String,Object> params,Model model) {
        PagePojo<Demo> page=new PagePojo<Demo>();
        page.setPageNo(1);
        PagePojo<Demo> demoPagePojo = pageService.queryByParamsPage(page,params);
        model.addAttribute("page",demoPagePojo);
        return "jsp/showDialogIndex";
    }
  @RequestMapping("j/testgit2")
    public String testgit2(@RequestBody Map<String,Object> params,Model model) {
        PagePojo<Demo> page=new PagePojo<Demo>();
        page.setPageNo(1);
        PagePojo<Demo> demoPagePojo = pageService.queryByParamsPage(page,params);
        model.addAttribute("page",demoPagePojo);
        return "jsp/showDialogIndex";
    }


}
